//
//  HeaderViewController.swift
//  TodayHistory
//
//  Created by RYX on 2018/12/5.
//  Copyright © 2018年 tuobu. All rights reserved.
//

import UIKit
import SnapKit
import Moya
import SwiftyJSON

class HeaderViewController: UIViewController {

    lazy var tableView = getTableView()
    
    var model: NewsModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            
            make.edges.equalTo(self.view)
        }
        
        loadData()
    }

}

extension HeaderViewController{
    
    func getTableView() -> UITableView {
        
        tableView = UITableView.init()
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.hxc_registerCell(xibName: "HeaderCell", identifier: "CELL")
        return tableView;
    }
    
    func loadData() -> Void {

        
//        let json = """
//                [{
//                 "fullName": "Federico Zanetello",
//                 "id": 123456,
//                 "twitter": "http://twitter.com/zntfdr"
//                },{
//                 "fullName": "Federico Zanetello",
//                 "id": 123456,
//                 "twitter": "http://twitter.com/zntfdr"
//                },{
//                 "fullName": "Federico Zanetello",
//                 "id": 123456,
//                 "twitter": "http://twitter.com/zntfdr"
//                }]
//                """.data(using: .utf8)! // our data in native format
//
//        let myStructArray = try! JSONDecoder().decode([NewsModel].self, from: json)
//
//        myStructArray.forEach { print($0) } // decoded!!!!!

        
        
        let provide = MoyaProvider<HttpRequest>()
        provide.request(.getNewsList(type: "top", key:"3e3df4d6980491ac999e1928118977d8")) {
            Result in switch Result {

                case let .success(response): //数据解析
                    
                    let decoder = JSONDecoder()
                    let model = try? decoder.decode(NewsModel.self, from: response.data)
                    
                    let json = JSON(response.data)
                    let news = json["result"]["data"]
//
                    
                    print("news")
                
                case let .failure(error):
                    print(error)

            }
        }
    }
}

extension HeaderViewController:UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 168
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 29
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.hxc_dequeueReusableCell(indexPath: indexPath, identifier: "CELL")
        
        return cell
    }
    
    
    
    
}
